var biaoben_code_counter = 0;

$(function(){
    // 长按弹出框
    $("[hedui_zhuangtai='未核对']").die().live("click",function(){
        var jiancha_id = $(this).attr("jiancha_id");
        var biaoben_tiaoma = $(this).attr("biaoben_tiaoma");
        var str_button = "";


        str_button += '<input class="zhixing_button" type="button" value="核对" />';
        str_button += '<input class="zhixing_button" id="yizhu_cancel" type="button" value="取消" />';

        $(".yizhu_dialog_title").html("未核对");
        $(".yizhu_dialog_content").html('<div class="jiancha_dialog biaoben" style="margin-bottom:20px;">'+$(this).html()+'</div>');
        $(".yizhu_dialog_button").html(str_button);
        $("#yizhu_dialog").show();

        biaoben_code_counter = 1;

        $(".zhixing_button").die().live("click",function(){
            zhixing_opration = $(this).val();
            if(zhixing_opration=="取消")
            {
                $("#yizhu_dialog").hide();
            }
            else
            {
                updateJianchaHeduiZhuangtai(biaoben_tiaoma,getRequestOne("user_number"));
                $("#yizhu_dialog").hide();
            }

            // 置零
            biaoben_code_counter = 0;
        });
    });

    
});

// 根据条件获取检查列表数据
function getJianchaList(zhuyuan_id,jiancha_hedui_zhuangtai)
{
    setHeader();
	$.ajax({
        type: "get",
        async: false,
        url:server_url+"Mobile/Jiancha/getJianchaListJson"+yiyuan_id,
        data: { zhuyuan_id:zhuyuan_id,jiancha_hedui_zhuangtai:jiancha_hedui_zhuangtai,user_number:getRequestOne("user_number")},
        dataType: "jsonp",
        jsonp: "callbackGetJianchaListJson",
        jsonpCallback:"feedBackStateGetJianchaListJson",
        error:function(mes)
        {
            alert(mes);
        },
        success: function(result) {
            if(result.ret=="success")
            {
                var jiancha_list = result.data;
                var str_html = '';
                if(jiancha_list!=null)
                {
                    for(var i=0;i<jiancha_list.length;i++)
                    {
                        str_html += '<div class="jiancha biaoben" hedui_zhuangtai="'+jiancha_list[i].jiancha_hedui_zhuangtai+'" jiancha_id="'+jiancha_list[i].id+'" biaoben_tiaoma="'+jiancha_list[i].biaoben_tiaoma+'">'+
                                        '<div class="jiancha_text jiancha_text1">检查名称:<span class="jiancha_name">'+jiancha_list[i].jiancha_mingcheng+'</span></div>'+
                                        '<div class="jiancha_text">送检物:<font color="red">'+jiancha_list[i].songjian_wu+'</font></div>'+
                                        // '<div class="jiancha_text"><span style="float:left">科室:'+jiancha_list[i].jiancha_keshi_name+'</span><span style="float:right">申请时间:'+jiancha_list[i].shenqing_time+'</span></div>'+
                                        '<div class="jiancha_text"><span style="float:left">科室:'+jiancha_list[i].jiancha_keshi_name+'</span></div>'+
                                        '<input type="hidden" value="'+jiancha_list[i].jiancha_hedui_zhuangtai+'"/>';
						if (jiancha_list[i].jiancha_hedui_zhuangtai == '已核对')
						{
							str_html += '<div><span style="color:#979797">核对人:'+jiancha_list[i].jiancha_hedui_hushi_name+'</span><span style="color:#979797;float:right">时间:'+jiancha_list[i].jiancha_hedui_time+'</span></div>';
						}
						str_html += '</div>';
                    }
                }
                
                $('.content').html(str_html);
            }
            else
            {
                alert(result.msg);
            }
        }
    });
}

// 更新检查核对状态
function updateJianchaHeduiZhuangtai(biaoben_tiaoma,user_number)
{
    $.ajax({
        type: "get",
        async: false,
        url:server_url+"Mobile/Jiancha/updateJianchaHeduiZhuangtaiJson"+yiyuan_id,
        data: { biaoben_tiaoma:biaoben_tiaoma,user_number:user_number,zhuyuan_id:patient_info_global.zhuyuan_id},
        dataType: "jsonp",
        jsonp: "callbackUpdateJianchaHeduiZhuangtaiJson",
        jsonpCallback:"feedBackStateUpdateJianchaHeduiZhuangtaiJson",
        error:function(mes)
        {
            alert(mes);
        },
        success: function(result) {
            if(result.ret=="success")
            {
                $("[biaoben_tiaoma='"+biaoben_tiaoma+"']").remove();
            }
            else
            {
                alert(result.msg);
            }
        }
    });
}

// 扫描标本瓶签操作
function saomaBiaobenPingqian(code)
{
    var zhuyuan_id = patient_info_global.zhuyuan_id;
    var biaoben_tiaoma = code;
    var current_zhixing_state = "";
    var zhixing_opration = "";
    // 首先获取当前执行状态
    $.ajax({
        type: "get",
        async: false,
        url:server_url+"Mobile/Jiancha/getJianchaHeduiZhuangtaiJson"+yiyuan_id,
        data: { biaoben_tiaoma:biaoben_tiaoma,zhuyuan_id:zhuyuan_id,user_number:nurse_info_global.user_number,user_department:nurse_info_global.user_department},
        dataType: "jsonp",
        jsonp: "callbackGetJianchaHeduiZhuangtaiJson",
        jsonpCallback:"feedBackStateGetJianchaHeduiZhuangtaiJson",
        error:function(mes)
        {
            alert("获取检查核对状态出错！");
        },
        success: function(result) {
            if(result.ret=="success")
            {
                // 第一次扫描弹窗
                if(biaoben_code_counter==0)
                {
                    var str_button = "";
                    str_button += '<input class="zhixing_button" type="button" value="核对" />';
                    str_button += '<input class="zhixing_button" id="yizhu_cancel" type="button" value="取消" />';
                    
                    var str_html = "";

                    $(".jiancha").each(function(){
                        if($(this).attr("biaoben_tiaoma")==biaoben_tiaoma)
                        {
                            str_html = $(this).html();
                        }
                    });

                    $(".yizhu_dialog_title").html("未核对");
                    $(".yizhu_dialog_content").html('<div class="jiancha biaoben">'+str_html+'</div>');
                    $(".yizhu_dialog_button").html(str_button);

                    $("#yizhu_dialog").show();
                    biaoben_code_counter = 1;

                    $(".zhixing_button").die().live("click",function(){
                        zhixing_opration = $(this).val();
                        if(zhixing_opration=="取消")
                        {
                            $("#yizhu_dialog").hide();
                        }
                        else
                        {
                            updateJianchaHeduiZhuangtai(biaoben_tiaoma,nurse_info_global.user_number);
                            $("#yizhu_dialog").hide();
                        }

                        // 置零
                        biaoben_code_counter = 0;
                    });
                    
                }
                // 第二次扫描执行
                else
                {
                    // 执行
                    updateJianchaHeduiZhuangtai(biaoben_tiaoma,nurse_info_global.user_number);
                    $("#yizhu_dialog").hide();

                    // 置零
                    biaoben_code_counter=0;
                }
            }
            else
            {
                alert(result.msg);
            }
        }
    });

}